package org.example.registrationcenter.listener;

import com.netflix.appinfo.InstanceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaServerStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component

public class ClientChangeListener {
    private final static Logger log = LoggerFactory.getLogger(ClientChangeListener.class);

    @EventListener
    public void registerListen(EurekaInstanceRegisteredEvent event) {
        InstanceInfo instanceInfo = event.getInstanceInfo();
        StringBuilder sb = new StringBuilder();
        sb.append("服务名=").append(instanceInfo.getAppName()).append("服务的host名=").
                append(instanceInfo.getHostName()).append("服务的ip地址=").
                append(instanceInfo.getIPAddr()).append("服务的端口=").append(instanceInfo.getPort()).append("服务ID=").
                append(instanceInfo.getInstanceId());
        log.info(sb.toString());
    }

    @EventListener
    public void canceledListen(EurekaInstanceCanceledEvent event) {
        log.info("服务名={}下线了", event.getAppName());
        log.info("server地址信息{}", event.getServerId());

    }

    @EventListener
    public void renewedListen(EurekaInstanceRenewedEvent event) {
        log.info("服务名={}进行续约", event.getServerId() +"  "+ event.getAppName());
    }

    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {
        log.info("注册中心启动,{}", System.currentTimeMillis());
    }

    @EventListener
    public void listen(EurekaServerStartedEvent event) {
        log.info("注册中心服务端启动,{}", System.currentTimeMillis());
    }
}
